package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
@Slf4j
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 当前用户发布动态
     * @return
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        log.info(movement.toString());

        movementService.movements(movement,imageContent);

        return ResponseEntity.ok(null);
    }

    /**
     * 查询当前用户id的所有动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findByIdMovements(Long userId,
                                            @RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int pagesize){

        PageResult pageResult = movementService.findByIdMovements(userId,page,pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询当前用户所有好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity findByFriendMovementAll(@RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int pagesize){

        PageResult pageResult = movementService.findByFriendMovementAll(page,pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询当前用户推荐好友的所有动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity findByRecommendM(@RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pagesize){

        PageResult pageResult = movementService.findByRecommendM(page,pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据动态id获取单条动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findMovement(@PathVariable("id") String movementId){
        MovementsVo movementsVo = movementService.findMovement(movementId);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 动态-点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity saveLike(@PathVariable("id") String movementId){
        Integer countLikes = movementService.saveLike(movementId);
        return ResponseEntity.ok(countLikes);
    }

    /**
     * 动态取消点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId){
        Integer countLike = movementService.disLike(movementId);
        return ResponseEntity.ok(countLike);
    }

    /**
     * 动态-喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity saveLove(@PathVariable("id") String movementId){
        Integer countLikes = movementService.saveLove(movementId);
        return ResponseEntity.ok(countLikes);
    }

    /**
     * 动态-取消喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
        Integer countLike = movementService.unlove(movementId);
        return ResponseEntity.ok(countLike);
    }

    /**
     * 谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> list = movementService.visitors();

        return ResponseEntity.ok(list);
    }
}
